

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>mindspore.ops.ScatterNd &mdash; MindSpore master documentation</title>
  

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/doctools.js"></script>
        <script src="../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="mindspore.ops.ScatterNdAdd" href="mindspore.ops.ScatterNdAdd.html" />
    <link rel="prev" title="mindspore.ops.ScatterMul" href="mindspore.ops.ScatterMul.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> MindSpore
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">MindSpore Python API</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.html">mindspore</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.common.initializer.html">mindspore.common.initializer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.communication.html">mindspore.communication</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.compression.html">mindspore.compression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.context.html">mindspore.context</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.html">mindspore.dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.audio.html">mindspore.dataset.audio</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.config.html">mindspore.dataset.config</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.text.html">mindspore.dataset.text</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.transforms.html">mindspore.dataset.transforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.vision.html">mindspore.dataset.vision</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.mindrecord.html">mindspore.mindrecord</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.nn.html">mindspore.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.nn.probability.html">mindspore.nn.probability</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.nn.transformer.html">mindspore.nn.transformer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.numpy.html">mindspore.numpy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.ops.html">mindspore.ops</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.parallel.html">mindspore.parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.parallel.nn.html">mindspore.parallel.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.profiler.html">mindspore.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.scipy.html">mindspore.scipy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.train.html">mindspore.train</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.boost.html">mindspore.boost</a></li>
</ul>
<p class="caption"><span class="caption-text">MindSpore C++ API</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://www.mindspore.cn/lite/api/zh-CN/master/api_cpp/mindspore.html">MindSpore Lite↗</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">MindSpore</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../mindspore.ops.html">mindspore.ops</a> &raquo;</li>
        
      <li>mindspore.ops.ScatterNd</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
          
            <a href="../../_sources/api_python/ops/mindspore.ops.ScatterNd.rst.txt" rel="nofollow"> View page source</a>
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="mindspore-ops-scatternd">
<h1>mindspore.ops.ScatterNd<a class="headerlink" href="#mindspore-ops-scatternd" title="Permalink to this headline">¶</a></h1>
<dl class="class">
<dt id="mindspore.ops.ScatterNd">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.ops.</code><code class="sig-name descname">ScatterNd</code><a class="reference internal" href="../../_modules/mindspore/ops/operations/array_ops.html#ScatterNd"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#mindspore.ops.ScatterNd" title="Permalink to this definition">¶</a></dt>
<dd><p>Scatters a tensor into a new tensor depending on the specified indices.</p>
<p>Creates an empty tensor with the given <cite>shape</cite>, and set values by scattering the update tensor
depending on indices.</p>
<p>The empty tensor has rank P and <cite>indices</cite> has rank Q where <cite>Q &gt;= 2</cite>.</p>
<p><cite>indices</cite> has shape <span class="math notranslate nohighlight">\((i_0, i_1, ..., i_{Q-2}, N)\)</span> where <cite>N &lt;= P</cite>.</p>
<p>The last dimension of <cite>indices</cite> (with length <cite>N</cite> ) indicates slices along the <cite>N</cite> th dimension of the empty tensor.</p>
<p><cite>updates</cite> is a tensor of rank <cite>Q-1+P-N</cite>. Its shape is: <span class="math notranslate nohighlight">\((i_0, i_1, ..., i_{Q-2}, shape_N, ..., shape_{P-1})\)</span>.</p>
<p>The following figure shows the calculation process of inserting two slices in the first dimension of a rank-3
with two matrices of new values:</p>
<img alt="api_python/ops/api_img/ScatterNd.png" src="api_python/ops/api_img/ScatterNd.png" />
<dl class="simple">
<dt>Inputs:</dt><dd><ul class="simple">
<li><p><strong>indices</strong> (Tensor) - The index of scattering in the new tensor with int32 or int64 data type.
The rank of indices must be at least 2 and <cite>indices_shape[-1] &lt;= len(shape)</cite>.</p></li>
<li><p><strong>updates</strong> (Tensor) - The source Tensor to be scattered.
It has shape <cite>indices_shape[:-1] + shape[indices_shape[-1]:]</cite>.</p></li>
<li><p><strong>shape</strong> (tuple[int]) - Define the shape of the output tensor, has the same data type as indices.
The shape of <cite>shape</cite> is <span class="math notranslate nohighlight">\((x_1, x_2, ..., x_R)\)</span>, and the length of ‘shape’ is greater than or equal to 2.
In other words, the shape of <cite>shape</cite> is at least <span class="math notranslate nohighlight">\((x_1, x_2)\)</span>.
And the value of any element in <cite>shape</cite> must be greater than or equal to 1.
In other words, <span class="math notranslate nohighlight">\(x_1\)</span> &gt;= 1, <span class="math notranslate nohighlight">\(x_2\)</span> &gt;= 1.</p></li>
</ul>
</dd>
<dt>Outputs:</dt><dd><p>Tensor, the new tensor, has the same type as <cite>update</cite> and the same shape as <cite>shape</cite>.</p>
</dd>
</dl>
<dl class="field-list simple">
<dt class="field-odd">Raises</dt>
<dd class="field-odd"><ul class="simple">
<li><p><a class="reference external" href="https://docs.python.org/library/exceptions.html#TypeError" title="(in Python v3.8)"><strong>TypeError</strong></a> – If <cite>shape</cite> is not a tuple.</p></li>
<li><p><a class="reference external" href="https://docs.python.org/library/exceptions.html#ValueError" title="(in Python v3.8)"><strong>ValueError</strong></a> – If any element of <cite>shape</cite> is less than 1.</p></li>
</ul>
</dd>
</dl>
<dl class="simple">
<dt>Supported Platforms:</dt><dd><p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">op</span> <span class="o">=</span> <span class="n">ops</span><span class="o">.</span><span class="n">ScatterNd</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">indices</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">2</span><span class="p">]]),</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">updates</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span>
<span class="gp">... </span>                            <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">]],</span>
<span class="gp">... </span>                           <span class="p">[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span>
<span class="gp">... </span>                            <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">]]]),</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">shape</span> <span class="o">=</span> <span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">op</span><span class="p">(</span><span class="n">indices</span><span class="p">,</span> <span class="n">updates</span><span class="p">,</span> <span class="n">shape</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
<span class="go">[[[1. 1. 1. 1.]</span>
<span class="go">  [2. 2. 2. 2.]</span>
<span class="go">  [3. 3. 3. 3.]</span>
<span class="go">  [4. 4. 4. 4.]]</span>
<span class="go"> [[0. 0. 0. 0.]</span>
<span class="go">  [0. 0. 0. 0.]</span>
<span class="go">  [0. 0. 0. 0.]</span>
<span class="go">  [0. 0. 0. 0.]]</span>
<span class="go"> [[1. 1. 1. 1.]</span>
<span class="go">  [2. 2. 2. 2.]</span>
<span class="go">  [3. 3. 3. 3.]</span>
<span class="go">  [4. 4. 4. 4.]]</span>
<span class="go"> [[0. 0. 0. 0.]</span>
<span class="go">  [0. 0. 0. 0.]</span>
<span class="go">  [0. 0. 0. 0.]</span>
<span class="go">  [0. 0. 0. 0.]]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">indices</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]]),</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">updates</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">3.2</span><span class="p">,</span> <span class="mf">1.1</span><span class="p">]),</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">shape</span> <span class="o">=</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">op</span><span class="p">(</span><span class="n">indices</span><span class="p">,</span> <span class="n">updates</span><span class="p">,</span> <span class="n">shape</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># In order to facilitate understanding, explain the operator pseudo-operation process step by step:</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Step 1: Generate an empty Tensor of the specified shape according to the shape</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># [</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1">#     [0. 0. 0.]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1">#     [0. 0. 0.]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1">#     [0. 0. 0.]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># ]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Step 2: Modify the data at the specified location according to the indicators</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># 0th row of indices is [0, 1], 0th row of updates is 3.2.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># means that the empty tensor in the 0th row and 1st col set to 3.2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># [</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1">#     [0. 3.2. 0.]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1">#     [0. 0.   0.]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1">#     [0. 0.   0.]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># ]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># 1th row of indices is [1, 1], 1th row of updates is 1.1.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># means that the empty tensor in the 1th row and 1st col set to 1.1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># [</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1">#     [0. 3.2. 0.]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1">#     [0. 1.1  0.]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1">#     [0. 0.   0.]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># ]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># The final result is as follows:</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
<span class="go">[[0. 3.2 0.]</span>
<span class="go"> [0. 1.1 0.]</span>
<span class="go"> [0. 0.  0.]]</span>
</pre></div>
</div>
</dd></dl>

</div>


           </div>
           
          </div>
          <footer>
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
        <a href="mindspore.ops.ScatterNdAdd.html" class="btn btn-neutral float-right" title="mindspore.ops.ScatterNdAdd" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
        <a href="mindspore.ops.ScatterMul.html" class="btn btn-neutral float-left" title="mindspore.ops.ScatterMul" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2021, MindSpore.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>